const software = require("../Model/software.model");
const {
  CreateServiceError,
  updateServiceError,
} = require("../Constant/err.type");
class softService {
  // 写入数据库接口service层
  async CreatesoftService(ctx, softName, iconUrl, softMenuInfo) {
    try {
      let res = await software.create({ softName, iconUrl, softMenuInfo });
      return res.dataValues;
    } catch (error) {
      CreateServiceError.result = error;
      ctx.app.emit("error", CreateServiceError, ctx);
      return false;
    }
  }
  async removesoftService(id) {
    const res = await software.destroy({ where: { id } });
    return res > 0 ? true : false;
  }
  async restoresoftService(id) {
    const res = await software.restore({ where: { id } });
    return res > 0 ? true : false;
  }
  async findsoftService() {
    const { count, rows } = await software.findAndCountAll({
      attributes: [
        "id",
        "softName",
        "iconUrl",
        "useTimes",
        "softFocus",
        "softMenuInfo",
        "softIsopen",
      ],
      offset: 0,
      limit: 1000,
    });
    return {
      total: count,
      list: rows,
    };
  }
  async updatesoftService(id, soft) {
    try {
      const res = await software.update(soft, { where: { id } });
      return res[0] !== 0 ? res[0] : null;
    } catch (error) {
      ctx.app.emit("error", updateServiceError, ctx);
      return null;
    }
  }
}
module.exports = new softService();
